package com.baseinfo.action;


import com.baseinfo.service.MessageService;
import com.framework.base.controller.BaseViewController;
import com.framework.view.KxdParams;
import com.framework.view.ResultDatas;
import com.framework.view.UserInfoView;
import com.model.TMessage;
import com.model.TSysRole;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Zhoubin.
 */
@Controller
@RequestMapping("/soa/baseinfo/message")
public class MessageController extends BaseViewController{
    Logger logger = Logger.getLogger(MessageController.class);

    @Resource(name="messageServiceImpl")
    MessageService messageService;

    /**
     * 跳转到首页
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value="index",method={ RequestMethod.POST,RequestMethod.GET})
    public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Object> params=new HashMap<String, Object>();
        UserInfoView<TSysRole> userInfo=getSessionUser(request);
        params.put("user", userInfo);
        return new ModelAndView("/baseinfo/message/messageindex",params);
    }

    @RequestMapping(value="update",method={ RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Object update(HttpServletRequest request, HttpServletResponse response,Long id) throws IOException {
        Map<String,Object> result=new HashMap<String,Object>();
        TMessage model = messageService.findById(id);
        if(model.getState()==0)
        {
            model.setState(1);
            result.put("msg", "启用成功");
            result.put("status", 0);
        }
        else if (model.getState()==1)
        {
            model.setState(0);
            result.put("msg", "禁用成功");
            result.put("status", 0);
        }else
        {
            result.put("msg", "状态异常!");
            result.put("status", 0);
        }

        messageService.saveOrupdate(model);
        return result;
    }

    @RequestMapping(value="datas/search",method={RequestMethod.POST})
    @ResponseBody
    public Object conditionsPaging(HttpServletRequest request, HttpServletResponse response, KxdParams params, Long start, Long length) throws IOException{
        ResultDatas resultDatas = new ResultDatas();
        Map<String,Object> map = new HashMap<String,Object>();
        if(null!=params.getParams()){
            map = params.getParams();
        }
        List<TMessage> list = messageService.findAll(map);
        if (list.size()>0) {
            resultDatas.setSuccess(0);
            resultDatas.setMsg("拿到数据");
            resultDatas.setData(list);
        }else{
            resultDatas.setSuccess(1);
            resultDatas.setData("");
            resultDatas.setMsg("没有数据");
        }
        list=null;
        return resultDatas;

    }
}
